package com.travelsmart.client.apresentacao.model;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.travelsmart.client.apresentacao.controller.Observable;
import com.travelsmart.client.apresentacao.i18n.I18nMessages;
import com.travelsmart.client.apresentacao.model.SmartHotelModel.LOAD_STATUS;
import com.travelsmart.client.bean.to.ReservaHotelTO;
import com.travelsmart.client.control.HotelSmartServiceAsync;

public class ReservaModel extends Observable<ReservaHotelTO> {

	private ReservaHotelTO reserva;

	public void setReserva(ReservaHotelTO reserva) {
		this.reserva = reserva;
		super.setChanged();
		super.notifyObservers(reserva);
	}

	/*/# INICIO - EXERC. 1
	
	//FIM - EXERC. 1 #*/
	

	public void criarReserva(final ReservaHotelTO reserva) {
		// INICIO - EXERC. 3
		SmartHotelModel.instance.getLoadModel().setLoad(LOAD_STATUS.LOADING);
		// FIM - EXERC. 3
		
		// INICIO - EXERC. 3
		HotelSmartServiceAsync.service.criarReservaHotel(reserva, 
				new AsyncCallback<ReservaHotelTO>() {
					@Override
					public void onSuccess(ReservaHotelTO result) {
						SmartHotelModel.instance.getLoadModel().setLoad(LOAD_STATUS.DONE);
						SmartHotelModel.instance.getMsgHotelModel().setMsg(I18nMessages.INSTANCE.MSG_SUCESSO_RESERVA());
					}
					
					@Override
					public void onFailure(Throwable caught) {
						SmartHotelModel.instance.getLoadModel().setLoad(LOAD_STATUS.DONE);
						SmartHotelModel.instance.getErroHotelModel().setErro(caught);
					}
				});
		// FIM - EXERC. 3
	}

	public void obterReserva(Long id) {
		assert id != 0 : "id = 0";
		
		SmartHotelModel.instance.getLoadModel().setLoad(LOAD_STATUS.LOADING);
		HotelSmartServiceAsync.service.obterReservaHotel(id, new AsyncCallback<ReservaHotelTO>() {

			@Override
			public void onFailure(Throwable caught) {
				SmartHotelModel.instance.getLoadModel().setLoad(LOAD_STATUS.DONE);
				SmartHotelModel.instance.getErroHotelModel().setErro(caught);
			}

			@Override
			public void onSuccess(ReservaHotelTO result) {
				SmartHotelModel.instance.getLoadModel().setLoad(LOAD_STATUS.DONE);
				setReserva(result);
			}
			
		});
		
	}
}
